package com.demo.zlb.sso.login.server.impl;

import com.alibaba.xxpt.gateway.shared.api.request.OapiMoziFusionGetPropertiesByOrgTypeCodeRequest;
import com.alibaba.xxpt.gateway.shared.api.response.OapiMoziFusionGetPropertiesByOrgTypeCodeResponse;
import com.alibaba.xxpt.gateway.shared.client.http.IntelligentGetClient;
import com.alibaba.xxpt.gateway.shared.client.http.PostClient;
import com.demo.zlb.sso.login.enums.RequestUrlEnum;
import com.demo.zlb.sso.login.server.DingServer;
import com.demo.zlb.sso.login.utils.DingRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author shiyuchen
 * @date 2023-04-12
 */
@Service
@Slf4j
public class DingServerImpl implements DingServer {
    /**
     * 租户id
     */
    @Value("${zzdingTalk.tenantId}")
    private Long tenantId;

    @Override
    public OapiMoziFusionGetPropertiesByOrgTypeCodeResponse getPropertiesByOrgTypeCode(String code) {
        //executableClient保证单例
        IntelligentGetClient intelligentGetClient = DingRequestUtil.getConnect().newIntelligentGetClient(RequestUrlEnum.PROPERTIES_BY_CODE.getUrl());

        OapiMoziFusionGetPropertiesByOrgTypeCodeRequest oapiMoziFusionGetPropertiesByOrgTypeCodeRequest = new OapiMoziFusionGetPropertiesByOrgTypeCodeRequest();
        // 设置参数
        //组织类型code
        oapiMoziFusionGetPropertiesByOrgTypeCodeRequest.setOrgTypeCode(code);
        //租户id
        oapiMoziFusionGetPropertiesByOrgTypeCodeRequest.setTenantId(tenantId);
        //获取结果
        OapiMoziFusionGetPropertiesByOrgTypeCodeResponse apiResult = intelligentGetClient.get(oapiMoziFusionGetPropertiesByOrgTypeCodeRequest);
        log.info("result:{}", apiResult);
        return apiResult;
    }

    @Override
    public String pageOrganizationEmployeeCodes(String pageSize, String currentPage, String code) {
        PostClient postClient = DingRequestUtil.getConnect().newPostClient(RequestUrlEnum.PAGE_ORGANIZATION_EMPLOYEE_CODES.getUrl());
        //设置参数
        postClient.addParameter("returnTotalSize", "true");
        /**
         * A - 查询有效的数据
         * F - 查询无效的数据
         * TOTAL - 查询所有的数据(包含已删除人员数据)
         */
        postClient.addParameter("employeePositionStatus", "A");
        postClient.addParameter("pageSize", pageSize);
        postClient.addParameter("pageNo", currentPage);
        postClient.addParameter("organizationCode", code);
        postClient.addParameter("tenantId", tenantId.toString());
        log.info("result:{}", postClient.post());
        //调用API
        return postClient.post();
    }
}
